﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Editor.UI;
using AG.Editor.TextureFontUI.Controls;
using AG.Editor.Core;
using AG.Editor.TextureFontUI.Windows;

namespace AG.Editor.TextureFontUI
{
    public partial class AGETextureFontMgrPanel : UserControl, IAGEMainComponent
    {
        AGTextureFontEditPanel _editPanel;
        public AGETextureFontMgrPanel()
        {
            InitializeComponent();

            _editPanel = new AGTextureFontEditPanel();
            _editPanel.Dock = DockStyle.Fill;
            this.splitContainer1.Panel2.Controls.Add(_editPanel);

            BindTextureFontTree();
        }

        public void OnActived(AGEMainMenuMidiator mmm)
        {
            mmm.Clear();

            ToolStripMenuItem m1 = mmm.AddMenu(new ToolStripMenuItem("字体管理"));
            ToolStripMenuItem miCreateFont = new ToolStripMenuItem("创建字体");
            miCreateFont.Click += new EventHandler(miCreateFont_Click);
            m1.DropDownItems.Add(miCreateFont);
        }

        void miCreateFont_Click(object sender, EventArgs e)
        {
            AGCreateTextureFontWindow window = new AGCreateTextureFontWindow();
            if (window.ShowDialog() == DialogResult.OK)
            {
                AGEContext.Current.EProject.AddTextureFont(window.TextureFont);
                BindTextureFontTree();
            }
        }

        private void BindTextureFontTree()
        {
            ctlTreeTextureFont.Nodes.Clear();
            for (int index = 0; index < AGEContext.Current.EProject.TextureFonts.Count; index++)
            {
                AGTextureFontRef tFont = AGEContext.Current.EProject.TextureFonts[index];
                TreeNode node = new TreeNode(tFont.Caption);
                node.Tag = tFont;
                ctlTreeTextureFont.Nodes.Add(node);
            }
        }

        private void ctlTreeTextureFont_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            AGTextureFontRef tfRef = e.Node.Tag as AGTextureFontRef;
            if (tfRef != null)
            {
                AGTextureFont tf = AGTools.Current.TextureFontStore.LoadTextureFont(AGEContext.Current.EProject, tfRef);
                _editPanel.SetTextureFont(tf);
            }
        }
    }
}
